package com.ushine.test.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @Author: yls
 * @Date 2022/3/15 13:19
 * @Description: 随机生成数据的长度
 * @Version 1.0
 **/
@Slf4j
@Component
public class StringUtils {

    private static Random random = null;

    static {
        try {
            random = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 成功标识
     */
    private static final String SUCCESS = "200";



    /**
     * 功能描述:
     * 〈 检测名称是否可以存在〉
     *
     * @param length 长度
     * @return : java.lang.String
     * @author : yls
     * @date : 2022/3/15 13:29
     */
    private static String checkLength(Integer length) {
        if (length == null) {
            return null;
        }
        if (length == 0) {
            return "";
        }
        return SUCCESS;
    }

    /**
     * 功能描述:
     * 〈自动生成名字（中文）〉
     *
     * @param length 生成名字的长度
     * @return : java.lang.String
     * @throws
     * @author : yls
     * @date : 2022/3/15 13:23
     */
    public String getRandomChinese(Integer length) {
        String checkLength = checkLength(length);
        if (!SUCCESS.equals(checkLength)) {
            return checkLength;
        }
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < length; i++) {
            String str = null;
            // 定义高低位
            int hightPos;
            int lowPos;
            // 获取高位值
            hightPos = (176 + Math.abs(random.nextInt(39)));
            // 获取低位值
            lowPos = (161 + Math.abs(random.nextInt(93)));
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                // 转成中文
                str = new String(b, "GBK");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ret.append(str);
        }
        return ret.toString();
    }

    /**
     * 功能描述:
     * 〈 生成随机用户名，数字和字母组成 〉
     *
     * @param length 名称的长度
     * @return : java.lang.String
     * @author : yls
     * @date : 2022/3/15 13:21
     */
    public String getStringRandom(int length) {
        String checkLength = checkLength(length);
        if (!SUCCESS.equals(checkLength)) {
            return checkLength;
        }
        StringBuilder val = new StringBuilder();
        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (random.nextInt(26) + temp));
            } else {
                val.append(random.nextInt(10));
            }
        }
        return val.toString();
    }
}
